package com.zkingsoft.actions.admin.lin;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.model.lin.LinType;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.lin.LinTypeService;
import com.zkingsoft.util.WebUtil;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-05-08 15:14
 */
@Controller
@RequestMapping(value = "admin/linType")
public class LinTypeController extends BaseController {

	@Resource
	private LinTypeService linTypeService;

	@Resource
	private MatrixAdapter matrixAdapter;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "LinType_BEV";

	public static final String fnCode = "LinType";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(LinType linType,
			PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(search);
		return showList(linTypeService, linType, pageVo);
	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(LinType linType) {
		if (linType.getTypeId() != null) {
			matrixAdapter.isBtnPermitted(edit);
			linType.setTypeId(((LinType) WebUtil.getSessionAttribute(BEV))
					.getTypeId());
			AjaxResult result = modify(linTypeService,
					WebUtil.getSessionAttribute(BEV), linType, "布草");
			WebUtil.removeSessionAttribute(BEV);
			return result;
		} else {
			matrixAdapter.isBtnPermitted(add);
			return add(linTypeService, linType, "布草");
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		matrixAdapter.isBtnPermitted(edit);
		LinType linType;
		if (id != null) {
			linType = linTypeService.findById(id);
			WebUtil.getRequest().setAttribute("obj", linType);
			WebUtil.setSessionAttribute(BEV, linType);
		}
		return "admin/lin/LinType-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		matrixAdapter.isBtnPermitted(del);
		return remove(linTypeService, keys);
	}

	/**
	 * 获取所有布草类型
	 * @param linType
	 * @return
	 */
	@RequestMapping(value = "/getAllTyps")
	@ResponseBody
	public AjaxResult getAllTyps(LinType linType) {
		
		List<LinType> dataList = linTypeService.findByModel(linType);
		return new AjaxResult(AjaxResult.STATUS_OK, dataList);
	}
	
	/**
	 * 根据id获取布草类型信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/getTypeById")
	@ResponseBody
	public AjaxResult getTypeById(Long id, Long hotel_id) {
		
		LinType linType = linTypeService.findByIdAndHotel(id, hotel_id);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, "");
		result.putInMap("linen", linType);
		return result;
	}
	
}